Import CSV Data into Mysql Database in Codeigniter

Import CSV Data into Mysql Database in Codeigniter

Import csv data in bulk via a CSV file is one of the essential features of any web application and will decrease the time needed to insert data one by one. Here we want to import data from the CSV file into the Mysql database in the Codeigniter framework. There is no library for a CSV file in Codeigniter, but we’ve checked the internet and we’ve found a library named ‘csvimport’ which is built to import CSV data into the Codeigniter application. So we’ve used the ‘csvimport’ library to import data from the CSV file to the Mysql database in the Codeigniter Application.

When you import bulk data from a CSV file and use the Codeigniter framework for web app development, you can use the Codeigniter Database library insert_batch() method. By using this approach in single query execution we can execute multiple Insert data queries. So, by using this method, we can import a large amount of data in a very short time, since by using the ‘csvimport’ library, we have an array of CSV file data, and then we use insert_batch() to directly import all data into a single query. So this is one of the benefits of the Codeigniter framework if you import large data.

In the article code, we will use the CodeIgniter ‘csvimport’ library to import user data from CSV file into the database. To explain the functionality of CodeIgniter CSV Import the following process will be implemented.

  • Fetch all the user’s data from the database and listed on the web page.
  • CSV file upload form.
  • Parse and import CSV data in the database.

Take a look at the files structure before you start implementing the import CSV data into the database in the CodeIgniter framework.

codeigniter_import_csv/

Step-1: CSV File Format

The CSV file will have 4 fields-Name, Email, Phone, Status, based on the structure of its database table.

Import CSV Data into Mysql Database in Codeigniter

Step-2: Create Database Table

Copy and Run the following MySql Database query to create the “users” table to store CSV file data.

Step-3: config/autoload.php

Define the commonly used library and helper in the config/autoload.php file to load automatically on every request.

Step-4: libraries/csvimport.php

The library allows you to read a CSV file in CodeIgniter application and convert CSV data in an array. You can import data from the CodeIgniter CSV file by using this “csvimport” class.

Step-5: controllers/Users.php

The Users controller handles the CSV data import process.

__construct() – Loads the required library (csvimport), helper (file), and model (user).

index() – List the users data.

import() – Import CSV or Excel file data to the database.

Step-6: models/User.php

The User model handles the database related works (Fetch and Insert).

Step-7: View/users/index.php

Here, all the existing user’s data is fetched from the database and listed on the webpage.

ALSO READ: How to Import MySQL Database from SQL File using PHP

Conclusion:

We know how to import CSV data into the Mysql table using PHP script if you don’t know then follow the above link. Now in this post, we have discussed this topic into Codeigniter Framework.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request

Related posts